package com.atguigu.juc.atomics;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicMarkableReference;

/**
 * @author yfstart
 * @create 2022-07-20 19:50
 */
public class AtomicMarkableReferenceDemo {
  static AtomicMarkableReference<Integer> markableReference = new AtomicMarkableReference<>(100, false);

  public static void main(String[] args) {
    new Thread(()->{
      boolean marked = markableReference.isMarked();
      System.out.println(Thread.currentThread().getName() + "\t 默认标识：" + marked);
      // 暂停1秒钟线程等待后面t2线程和我拿到一样的模式flag标识，都是false
      try { TimeUnit.MILLISECONDS.sleep(1000); } catch (InterruptedException e) { e.printStackTrace();}
      markableReference.compareAndSet(100,1000,marked,!marked);
    },"t1").start();

    new Thread(()->{
      boolean marked = markableReference.isMarked();
      System.out.println(Thread.currentThread().getName() + "\t 默认标识：" + marked);
      try { TimeUnit.MILLISECONDS.sleep(2000); } catch (InterruptedException e) { e.printStackTrace();}
      boolean b = markableReference.compareAndSet(100, 2000, marked, !marked);
      System.out.println(Thread.currentThread().getName() + "\t t2线程的CAS结果为：" + b);
      System.out.println(Thread.currentThread().getName() + "\t" + markableReference.isMarked());
      System.out.println(Thread.currentThread().getName() + "\t" + markableReference.getReference());
    },"t2").start();
  }
}
